﻿/// <reference path="../vsdoc/jquery-1.8.3-vsdoc.js" />

/*
* settings for the FORM element
*/

var _mainForm = "mainForm";
var _listForm = "listForm";


/* Send FORM on ENTER press */
asyncHandlersForEach.push(AppendSendOnEnter);
function AppendSendOnEnter()
{
    $("FORM INPUT:text, FORM SELECT").keydown(SubmitForm);
};
function SubmitForm(e)
{
    if (e && e.keyCode == 13 && (e.result == null || e.result))
    {
        if (e.isDefaultPrevented())
        {
            return false;
        }
        e.preventDefault();
        $(this).parents("FORM").submit();
    }
}

$(document).ready(function ()
{
    $("A[href]").live('click', function (e)
    {
        if (e.result == null || e.result) { $("#loading").show(); }
    });
    $("FORM").submit(function (e)
    {
        if (e.result == null || e.result) { $("#loading").show(); }
    });
});

/* MainForm GetFocus */
asyncHandlersForEach.push(SetFocusToFirstFormInput);
function SetFocusToFirstFormInput()
{
    var element = $("#" + _mainForm + " INPUT[type=text], #" + _mainForm + " SELECT").get(0);
    try
    {
        if (element != null) { element.focus(); }
    } catch (ex) { }
}

// submit buttons OUT of the FORM element, triggers submit
$(document).ready(function ()
{
    $("BUTTON").live('click', function (e) // BUTTON[form] does not work for not html 5 browsers
    {
        var attrForm = $(this).attr("form");
        if (attrForm == null || typeof attrForm != "string")
        {
            return true;
        }
        if (e.isDefaultPrevented())
        {
            return false;
        }
        var form = $("FORM[id=" + attrForm + "]");
        form.append($("<INPUT>") // append the button name to FORM, for later use
                .attr("name", "SubmitButtonName")
                .attr("type", "hidden")
                .val($(this).attr("name")));
        form.submit();
        return false;
    });
});